package com.lff.mapper;

import com.lff.domain.Role;
import com.lff.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    //用户登录
    @Select("select * from smbms_user where userCode=#{userCode} and userPassword=#{userPassword}")
    User findUserByUsernameAndPassword(@Param("userCode") String userCode, @Param("userPassword") String userPassword);

    //检验密码
    @Select("select * from smbms_user where id=#{id} and userPassword=#{oldPassword}")
    User findUserByUserIdAndPassword(@Param("id") Long id, @Param("oldPassword") String oldPassword);

    //修改密码
    @Update("update smbms_user set userPassword=#{newpassword} where id=#{id}")
    int changePwd(@Param("id") Long id, @Param("newpassword") String newpassword);

    //根据用户id查询用户
    @Select("select * from smbms_user where id=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "userCode", column = "userCode"),
            @Result(property = "userName", column = "userName"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "createdBy", column = "createdBy"),
            @Result(property = "creationDate", column = "creationDate"),
            @Result(property = "modifyBy", column = "modifyBy"),
            @Result(property = "modifyDate", column = "modifyDate"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "address", column = "address"),
            @Result(javaType = Role.class,
                    property = "userRole",
                    column = "userRole",
                    one = @One(select = "com.lff.mapper.RoleMapper.findRoleById")
            )
    })
    User findById(@Param("id") Integer id);

    //查询用户列表
    @Select(" <script> " +
            "select * from smbms_user" +
            " <where> " +
            " <if test=\"queryname!=null\"> And userName like concat(\'%\',#{queryname},\'%\')</if> " +
            " <if test=\"queryUserRole!=null and queryUserRole!=0\"> And userRole=#{queryUserRole}</if> " +
            " </where> " +
            " </script> ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "userCode", column = "userCode"),
            @Result(property = "userName", column = "userName"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "createdBy", column = "createdBy"),
            @Result(property = "creationDate", column = "creationDate"),
            @Result(property = "modifyBy", column = "modifyBy"),
            @Result(property = "modifyDate", column = "modifyDate"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "address", column = "address"),
            @Result(property = "userRole",
                    javaType = Role.class,
                    column = "userRole",
                    one = @One(select = "com.lff.mapper.RoleMapper.findRoleById")
            )
    })
    List<User> findAll(@Param("queryname") String queryname, @Param("queryUserRole") Integer queryUserRole);


    @Update({"<script>" +
            "update smbms_user" +
            "<set>" +
            "<if test=\"userName!=null\">userName=#{userName},</if>" +
            "<if test=\"gender!=0\">gender=#{gender},</if>" +
            "<if test=\"birthday!=null\">birthday=#{birthday},</if>" +
            "<if test=\"phone!=null\">phone=#{phone},</if>" +
            "<if test=\"address!=null\">address=#{address},</if>" +
            "<if test=\"userRole!=null\">userRole=#{userRole.id}</if>" +
            "</set>" +
            "where id=#{id}" +
            "</script>"})
    void updateUser(User user);

    @Delete("delete from smbms_user where id=#{id}")
    Integer deleteById(Long id);

    /*---------------------------------用户创建模块-------------------------------*/

    //检查用户代码是否存在，
    @Select("select * from smbms_user where userCode=#{userCode}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "userCode", column = "userCode"),
            @Result(property = "userName", column = "userName"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "createdBy", column = "createdBy"),
            @Result(property = "creationDate", column = "creationDate"),
            @Result(property = "modifyBy", column = "modifyBy"),
            @Result(property = "modifyDate", column = "modifyDate"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "address", column = "address"),
            @Result(javaType = Role.class,
                    property = "userRole",
                    column = "userRole",
                    one = @One(select = "com.lff.mapper.RoleMapper.findRoleById")
            )
    })
    User findUserByUserCode(String userCode);


    @Insert("insert into smbms_user(userCode,userName,userPassword,gender,birthday,phone,address,userRole,createdBy,creationDate) " +
            "values(#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole.id},#{createdBy},#{creationDate})")
    void save(User user);
}
